/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.basic.service.impl;

import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.basic.service.BasicAc02Service;
import me.zhengjie.modules.basic.domain.dto.BasicAc02QueryCriteria;
import me.zhengjie.modules.basic.mapper.BasicAc02Mapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author ly
* @date 2025-04-22
**/
@Service
@RequiredArgsConstructor
public class BasicAc02ServiceImpl extends ServiceImpl<BasicAc02Mapper, BasicAc02> implements BasicAc02Service {

    private final BasicAc02Mapper basicAc02Mapper;

    @Override
    public PageResult<BasicAc02> queryAll(BasicAc02QueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(basicAc02Mapper.findAll(criteria, page));
    }

    @Override
    public List<BasicAc02> getAc02(BasicAc02 criteria){
        return basicAc02Mapper.getAc02(criteria);
    }

    @Override
    public List<BasicAc02> queryAll(BasicAc02QueryCriteria criteria){
        return basicAc02Mapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BasicAc02 resources) {
        resources.setAae011(SecurityUtils.getCurrentUsername());
        resources.setAae036(DateUtil.getStringCurDateTime());
        resources.setAab004(null);
        save(resources);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BasicAc02 resources) {
        BasicAc02 basicAc02 =basicAc02Mapper.selectByAaz159(resources);
        basicAc02.copy(resources);
        resources.setAae011(SecurityUtils.getCurrentUsername());
        resources.setAae036(DateUtil.getStringCurDateTime());
        basicAc02Mapper.updateByAaz159(basicAc02);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        basicAc02Mapper.deleteByAaz159(ids);
    }

    @Override
    public void check(Map map) {
        ValidateUtil.isEmptyThrowException(map.get("aac001"),"个人编号");
        ValidateUtil.isEmptyThrowException(map.get("aab001"),"单位编号");
        ValidateUtil.isEmptyThrowException(map.get("aae140"),"险种类型");

        BasicAc02 basicAc02 = new BasicAc02();
        basicAc02.setAac001(map.get("aac001").toString());
        basicAc02.setAab001(map.get("aab001").toString());
        basicAc02.setAae140(map.get("aae140").toString());
        List<BasicAc02> basicAc02s =basicAc02Mapper.getAc02(basicAc02);
        if (!ValidateUtil.isEmpty(basicAc02s)) {
            throw new RuntimeException("人员参保信息已存在，直接变更即可");
        }

    }

    @Override
    public void download(List<BasicAc02> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BasicAc02 basicAc02 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("个人编号", basicAc02.getAac001());
            map.put("建账年月", basicAc02.getAae206());
            map.put("参保日期", basicAc02.getAac030());
            map.put("参保状态", basicAc02.getAac008());
            map.put("缴费状态", basicAc02.getAac031());
            map.put("参保险种", basicAc02.getAae140());
            map.put("工资", basicAc02.getAac040());
            map.put("缴费开始年月", basicAc02.getAae041());
            map.put("缴费截止年月", basicAc02.getAae042());
            map.put("经办机构", basicAc02.getAab359());
            map.put("参保身份", basicAc02.getAac066());
            map.put("备注", basicAc02.getAae013());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
